Page({
  data: {
    showImageSwitch: false,
    index: null,
    imgList: [],
  },

  onLoad() {
    this.setData({ user: wx.appData.user })
  },

  ImageChange(e) {
    this.setData({ showImageSwitch: e.detail.value });
  },

  ChooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '哦豁',
      content: '确定要把这张照片删掉嘛？',
      cancelText: '还能用',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },

  submit(e) {
    let f = e.detail.value;
    let code = f.code;
    delete f.code;
    if (!(f.kd_name && f.kd_phone && f.building && f.floor && f.room)) {
      wx.toast('您的信息未填写完整');
      return 0;
    }
    f.unionid = wx.appData.user.unionid;
    if (this.data.imgList.length) {
      wx.uploadFile({
        url: wx.Config.host + 'uploadImage',
        filePath: this.data.imgList[0],
        name: 'image',
        formData: {
          'unionid': f.unionid,
        },
      })
    }
    wx.post('modifyUser', f, res => {
      wx.toast('保存成功', 1, 1, () => {
        wx.navigateBack({
          delta: 1,
        })
      });
    });
    if (code) {
      wx.login({
        success: res => {
          wx.post("wxStaffRegister", {
            code: res.code,
            unionid: wx.appData.user.unionid,
            verification: code
          }, res => {
            if (res.ok) {
              wx.reLaunch({ url: '/pages/index/index' });
            }
          })
        }
      })
    }
  }

})